<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>30s code string | 皮卡车</title>
    <meta name="description" content="皮卡车的文档, vuepress 文档">
    <meta name="generator" content="VuePress 1.3.1">
    <link rel="apple-touch-icon" href="/apple-touch-icon.png">
  <link rel="icon" href="/favicon.ico">
  <link rel="manifest" href="/manifest.json">
  <meta name="theme-color" content="#ffffff">
  <meta name="google-site-verification" content="Ld_AEWr30siza2LmmZ12csitRGsSj9gqqEAch0UXkTc">
    
    <link rel="preload" href="/assets/css/0.styles.1a6a4f51.css" as="style"><link rel="preload" href="/assets/js/app.1f0f93c9.js" as="script"><link rel="preload" href="/assets/js/2.f28f3227.js" as="script"><link rel="preload" href="/assets/js/69.48c9a47f.js" as="script"><link rel="preload" href="/assets/js/3.a7eaa85f.js" as="script"><link rel="prefetch" href="/assets/js/10.7b76d38f.js"><link rel="prefetch" href="/assets/js/100.0d62a3f6.js"><link rel="prefetch" href="/assets/js/101.4916ac0c.js"><link rel="prefetch" href="/assets/js/102.0ee21f66.js"><link rel="prefetch" href="/assets/js/103.872c594c.js"><link rel="prefetch" href="/assets/js/104.0bff815e.js"><link rel="prefetch" href="/assets/js/105.46330c43.js"><link rel="prefetch" href="/assets/js/106.4ad211ca.js"><link rel="prefetch" href="/assets/js/107.d9011871.js"><link rel="prefetch" href="/assets/js/108.bab8ed0e.js"><link rel="prefetch" href="/assets/js/109.b7e4a790.js"><link rel="prefetch" href="/assets/js/11.518e0dbb.js"><link rel="prefetch" href="/assets/js/110.1eb492e8.js"><link rel="prefetch" href="/assets/js/111.ee6a2e31.js"><link rel="prefetch" href="/assets/js/112.05d655c6.js"><link rel="prefetch" href="/assets/js/113.0a4cf8f0.js"><link rel="prefetch" href="/assets/js/114.ef783947.js"><link rel="prefetch" href="/assets/js/115.11bbdb9c.js"><link rel="prefetch" href="/assets/js/116.5646126d.js"><link rel="prefetch" href="/assets/js/117.854d26bf.js"><link rel="prefetch" href="/assets/js/118.1e514b13.js"><link rel="prefetch" href="/assets/js/119.aa944f45.js"><link rel="prefetch" href="/assets/js/12.511e3547.js"><link rel="prefetch" href="/assets/js/120.1c6f450c.js"><link rel="prefetch" href="/assets/js/121.44f16ee6.js"><link rel="prefetch" href="/assets/js/122.f877b8e8.js"><link rel="prefetch" href="/assets/js/123.0563fdd5.js"><link rel="prefetch" href="/assets/js/124.4cd97316.js"><link rel="prefetch" href="/assets/js/125.a502f4ee.js"><link rel="prefetch" href="/assets/js/126.c7e94093.js"><link rel="prefetch" href="/assets/js/127.bd8aed7f.js"><link rel="prefetch" href="/assets/js/128.66063cc3.js"><link rel="prefetch" href="/assets/js/129.21bb1073.js"><link rel="prefetch" href="/assets/js/13.9e5bee4e.js"><link rel="prefetch" href="/assets/js/130.c8edfccb.js"><link rel="prefetch" href="/assets/js/131.7a1f29be.js"><link rel="prefetch" href="/assets/js/132.f589fc06.js"><link rel="prefetch" href="/assets/js/133.55ac5c9a.js"><link rel="prefetch" href="/assets/js/134.eeab127b.js"><link rel="prefetch" href="/assets/js/135.5a1571dd.js"><link rel="prefetch" href="/assets/js/136.b72b4d1c.js"><link rel="prefetch" href="/assets/js/137.678e022a.js"><link rel="prefetch" href="/assets/js/138.ba4108fa.js"><link rel="prefetch" href="/assets/js/139.0b02b599.js"><link rel="prefetch" href="/assets/js/14.b9ec07e6.js"><link rel="prefetch" href="/assets/js/140.690e4e4c.js"><link rel="prefetch" href="/assets/js/141.709da4ed.js"><link rel="prefetch" href="/assets/js/142.f2de76c0.js"><link rel="prefetch" href="/assets/js/143.4dcb17f0.js"><link rel="prefetch" href="/assets/js/144.730ef429.js"><link rel="prefetch" href="/assets/js/145.d881c754.js"><link rel="prefetch" href="/assets/js/146.f3379a41.js"><link rel="prefetch" href="/assets/js/147.eeb9ca6f.js"><link rel="prefetch" href="/assets/js/148.bb6020cd.js"><link rel="prefetch" href="/assets/js/149.1d34dea6.js"><link rel="prefetch" href="/assets/js/15.d0fa1a9d.js"><link rel="prefetch" href="/assets/js/150.599238ba.js"><link rel="prefetch" href="/assets/js/151.9655268f.js"><link rel="prefetch" href="/assets/js/152.1f843fa4.js"><link rel="prefetch" href="/assets/js/153.7af45ca6.js"><link rel="prefetch" href="/assets/js/154.eff93a56.js"><link rel="prefetch" href="/assets/js/155.ff07fba4.js"><link rel="prefetch" href="/assets/js/156.1b1814b7.js"><link rel="prefetch" href="/assets/js/157.8ac4c21b.js"><link rel="prefetch" href="/assets/js/158.aa8a0124.js"><link rel="prefetch" href="/assets/js/159.d61fdb37.js"><link rel="prefetch" href="/assets/js/16.ccd2bd60.js"><link rel="prefetch" href="/assets/js/17.ddc8fde4.js"><link rel="prefetch" href="/assets/js/18.a06e60cc.js"><link rel="prefetch" href="/assets/js/19.64b3dec9.js"><link rel="prefetch" href="/assets/js/20.3615e182.js"><link rel="prefetch" href="/assets/js/21.0c654d53.js"><link rel="prefetch" href="/assets/js/22.720aac8f.js"><link rel="prefetch" href="/assets/js/23.0dc53b91.js"><link rel="prefetch" href="/assets/js/24.d4b75b0e.js"><link rel="prefetch" href="/assets/js/25.9120702e.js"><link rel="prefetch" href="/assets/js/26.b24d89e2.js"><link rel="prefetch" href="/assets/js/27.55df94ab.js"><link rel="prefetch" href="/assets/js/28.250c68a5.js"><link rel="prefetch" href="/assets/js/29.75a3c8f3.js"><link rel="prefetch" href="/assets/js/30.8cdc2fcd.js"><link rel="prefetch" href="/assets/js/31.45303d68.js"><link rel="prefetch" href="/assets/js/32.f4225739.js"><link rel="prefetch" href="/assets/js/33.15b21267.js"><link rel="prefetch" href="/assets/js/34.5baca30b.js"><link rel="prefetch" href="/assets/js/35.5d9dfab1.js"><link rel="prefetch" href="/assets/js/36.981eadca.js"><link rel="prefetch" href="/assets/js/37.cead7f6b.js"><link rel="prefetch" href="/assets/js/38.ee6bcaf1.js"><link rel="prefetch" href="/assets/js/39.e7579788.js"><link rel="prefetch" href="/assets/js/4.494c614b.js"><link rel="prefetch" href="/assets/js/40.01d2b30b.js"><link rel="prefetch" href="/assets/js/41.19cf06ce.js"><link rel="prefetch" href="/assets/js/42.15941cde.js"><link rel="prefetch" href="/assets/js/43.66adb73c.js"><link rel="prefetch" href="/assets/js/44.8d99ed62.js"><link rel="prefetch" href="/assets/js/45.12ea9159.js"><link rel="prefetch" href="/assets/js/46.644efa88.js"><link rel="prefetch" href="/assets/js/47.531131c7.js"><link rel="prefetch" href="/assets/js/48.b214f000.js"><link rel="prefetch" href="/assets/js/49.e42d4b72.js"><link rel="prefetch" href="/assets/js/5.7e277dc8.js"><link rel="prefetch" href="/assets/js/50.de0aa85c.js"><link rel="prefetch" href="/assets/js/51.8be2974c.js"><link rel="prefetch" href="/assets/js/52.cc067888.js"><link rel="prefetch" href="/assets/js/53.4ceb2c6b.js"><link rel="prefetch" href="/assets/js/54.b2647058.js"><link rel="prefetch" href="/assets/js/55.dc3df953.js"><link rel="prefetch" href="/assets/js/56.921a6934.js"><link rel="prefetch" href="/assets/js/57.1e24b1fd.js"><link rel="prefetch" href="/assets/js/58.f4e8ebac.js"><link rel="prefetch" href="/assets/js/59.17ab117b.js"><link rel="prefetch" href="/assets/js/6.4014218f.js"><link rel="prefetch" href="/assets/js/60.352cea9b.js"><link rel="prefetch" href="/assets/js/61.77289388.js"><link rel="prefetch" href="/assets/js/62.ec0ed919.js"><link rel="prefetch" href="/assets/js/63.c75dcb25.js"><link rel="prefetch" href="/assets/js/64.59d15a75.js"><link rel="prefetch" href="/assets/js/65.7e5a14bb.js"><link rel="prefetch" href="/assets/js/66.4e770ba1.js"><link rel="prefetch" href="/assets/js/67.0742f21a.js"><link rel="prefetch" href="/assets/js/68.a6ed34fd.js"><link rel="prefetch" href="/assets/js/7.c5ecd7d3.js"><link rel="prefetch" href="/assets/js/70.eb477192.js"><link rel="prefetch" href="/assets/js/71.1be57009.js"><link rel="prefetch" href="/assets/js/72.6859561c.js"><link rel="prefetch" href="/assets/js/73.aae65018.js"><link rel="prefetch" href="/assets/js/74.59d76d34.js"><link rel="prefetch" href="/assets/js/75.04e23427.js"><link rel="prefetch" href="/assets/js/76.74d6eb6a.js"><link rel="prefetch" href="/assets/js/77.fba76057.js"><link rel="prefetch" href="/assets/js/78.0da94b25.js"><link rel="prefetch" href="/assets/js/79.14676976.js"><link rel="prefetch" href="/assets/js/8.08c85334.js"><link rel="prefetch" href="/assets/js/80.7494721f.js"><link rel="prefetch" href="/assets/js/81.f04f6229.js"><link rel="prefetch" href="/assets/js/82.e4c6f74b.js"><link rel="prefetch" href="/assets/js/83.81ad8dbf.js"><link rel="prefetch" href="/assets/js/84.d28e4a3d.js"><link rel="prefetch" href="/assets/js/85.d99fa7fa.js"><link rel="prefetch" href="/assets/js/86.597ecadb.js"><link rel="prefetch" href="/assets/js/87.e6d21734.js"><link rel="prefetch" href="/assets/js/88.b43a52d4.js"><link rel="prefetch" href="/assets/js/89.a5ba287d.js"><link rel="prefetch" href="/assets/js/9.658cd6b3.js"><link rel="prefetch" href="/assets/js/90.c253df12.js"><link rel="prefetch" href="/assets/js/91.8946a93d.js"><link rel="prefetch" href="/assets/js/92.8b8f1ae0.js"><link rel="prefetch" href="/assets/js/93.aa73c784.js"><link rel="prefetch" href="/assets/js/94.9326bcc7.js"><link rel="prefetch" href="/assets/js/95.46b64b79.js"><link rel="prefetch" href="/assets/js/96.61a9b0b9.js"><link rel="prefetch" href="/assets/js/97.65a03aad.js"><link rel="prefetch" href="/assets/js/98.7aa56ed0.js"><link rel="prefetch" href="/assets/js/99.a652c8d5.js">
    <link rel="stylesheet" href="/assets/css/0.styles.1a6a4f51.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">皮卡车</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">
  Home
</a></div><div class="nav-item"><a href="/guide/" class="nav-link">
  Guide
</a></div><div class="nav-item"><a href="/computer/" class="nav-link">
  计算机
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="系统" class="dropdown-title"><span class="title">系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/os/linux/" class="nav-link">
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/os/manjaro/" class="nav-link">
  Manjaro
</a></li><li class="dropdown-item"><!----> <a href="/os/ubuntu/" class="nav-link">
  Ubuntu
</a></li><li class="dropdown-item"><!----> <a href="/os/centos/" class="nav-link">
  CentOS
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><span class="title">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/frontend/javascript/" class="nav-link">
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="https://css.shanyuhai.top/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSS
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="/frontend/webpack/" class="nav-link">
  Webpack
</a></li><li class="dropdown-item"><!----> <a href="/frontend/d3js/" class="nav-link">
  D3
</a></li><li class="dropdown-item"><!----> <a href="/frontend/utils/" class="nav-link router-link-active">
  Utils
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="后端" class="dropdown-title"><span class="title">后端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/backend/nodejs/" class="nav-link">
  Nodejs
</a></li><li class="dropdown-item"><!----> <a href="/backend/koa/" class="nav-link">
  Koa
</a></li><li class="dropdown-item"><!----> <a href="/backend/mongodb/" class="nav-link">
  MongoDB
</a></li><li class="dropdown-item"><!----> <a href="/backend/nginx/" class="nav-link">
  Nginx
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开发工具" class="dropdown-title"><span class="title">开发工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/tools/git/" class="nav-link">
  Git
</a></li><li class="dropdown-item"><!----> <a href="/tools/github/" class="nav-link">
  Github
</a></li><li class="dropdown-item"><!----> <a href="/tools/vscode/" class="nav-link">
  VSCode
</a></li><li class="dropdown-item"><!----> <a href="/tools/chrome/" class="nav-link">
  Chrome Developer tools
</a></li><li class="dropdown-item"><!----> <a href="/tools/bookmark-scripts/" class="nav-link">
  Bookmark scripts
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><span class="title">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/more/algorithm/" class="nav-link">
  算法
</a></li><li class="dropdown-item"><!----> <a href="/more/interview/" class="nav-link">
  面试题
</a></li><li class="dropdown-item"><!----> <a href="/more/hodgepodge/" class="nav-link">
  大杂烩
</a></li><li class="dropdown-item"><!----> <a href="/more/clean/" class="nav-link">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="https://v1.vuepress.vuejs.org/zh/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  VuePress1.x 官网
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">
  Home
</a></div><div class="nav-item"><a href="/guide/" class="nav-link">
  Guide
</a></div><div class="nav-item"><a href="/computer/" class="nav-link">
  计算机
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="系统" class="dropdown-title"><span class="title">系统</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/os/linux/" class="nav-link">
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/os/manjaro/" class="nav-link">
  Manjaro
</a></li><li class="dropdown-item"><!----> <a href="/os/ubuntu/" class="nav-link">
  Ubuntu
</a></li><li class="dropdown-item"><!----> <a href="/os/centos/" class="nav-link">
  CentOS
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><span class="title">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/frontend/javascript/" class="nav-link">
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="https://css.shanyuhai.top/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  CSS
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="/frontend/webpack/" class="nav-link">
  Webpack
</a></li><li class="dropdown-item"><!----> <a href="/frontend/d3js/" class="nav-link">
  D3
</a></li><li class="dropdown-item"><!----> <a href="/frontend/utils/" class="nav-link router-link-active">
  Utils
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="后端" class="dropdown-title"><span class="title">后端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/backend/nodejs/" class="nav-link">
  Nodejs
</a></li><li class="dropdown-item"><!----> <a href="/backend/koa/" class="nav-link">
  Koa
</a></li><li class="dropdown-item"><!----> <a href="/backend/mongodb/" class="nav-link">
  MongoDB
</a></li><li class="dropdown-item"><!----> <a href="/backend/nginx/" class="nav-link">
  Nginx
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="开发工具" class="dropdown-title"><span class="title">开发工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/tools/git/" class="nav-link">
  Git
</a></li><li class="dropdown-item"><!----> <a href="/tools/github/" class="nav-link">
  Github
</a></li><li class="dropdown-item"><!----> <a href="/tools/vscode/" class="nav-link">
  VSCode
</a></li><li class="dropdown-item"><!----> <a href="/tools/chrome/" class="nav-link">
  Chrome Developer tools
</a></li><li class="dropdown-item"><!----> <a href="/tools/bookmark-scripts/" class="nav-link">
  Bookmark scripts
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><span class="title">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/more/algorithm/" class="nav-link">
  算法
</a></li><li class="dropdown-item"><!----> <a href="/more/interview/" class="nav-link">
  面试题
</a></li><li class="dropdown-item"><!----> <a href="/more/hodgepodge/" class="nav-link">
  大杂烩
</a></li><li class="dropdown-item"><!----> <a href="/more/clean/" class="nav-link">
  风格指南
</a></li><li class="dropdown-item"><!----> <a href="https://v1.vuepress.vuejs.org/zh/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  VuePress1.x 官网
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>UTILS</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/frontend/utils/" class="sidebar-link">首页</a></li><li><a href="/frontend/utils/code-adapter.html" class="sidebar-link">30s code adapter</a></li><li><a href="/frontend/utils/code-archived.html" class="sidebar-link">30s code archived</a></li><li><a href="/frontend/utils/code-array.html" class="sidebar-link">30s code array</a></li><li><a href="/frontend/utils/code-browser.html" class="sidebar-link">30s code browser</a></li><li><a href="/frontend/utils/code-date.html" class="sidebar-link">30s code date</a></li><li><a href="/frontend/utils/code-function.html" class="sidebar-link">30s code function</a></li><li><a href="/frontend/utils/code-math.html" class="sidebar-link">30s code math</a></li><li><a href="/frontend/utils/code-node.html" class="sidebar-link">30s code node</a></li><li><a href="/frontend/utils/code-object.html" class="sidebar-link">30s code object</a></li><li><a href="/frontend/utils/code-string.html" class="active sidebar-link">30s code string</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#csvtoarray" class="sidebar-link">CSVToArray</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#csvtojson" class="sidebar-link">CSVToJSON</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#urljoin" class="sidebar-link">URLJoin</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#bytesize" class="sidebar-link">byteSize</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#capitalize-decapitalize" class="sidebar-link">capitalize/decapitalize</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#capitalizeeveryword" class="sidebar-link">capitalizeEveryWord</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#compactwhitespace" class="sidebar-link">compactWhitespace</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#escapehtml" class="sidebar-link">escapeHTML</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#escaperegexp" class="sidebar-link">escapeRegExp</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#fromcamelcase" class="sidebar-link">fromCamelCase</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#indentstring" class="sidebar-link">indentString</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#isabsoluteurl" class="sidebar-link">isAbsoluteURL</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#isanagram" class="sidebar-link">isAnagram</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#islowercase-isuppercase" class="sidebar-link">isLowerCase/isUpperCase</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#mapstring" class="sidebar-link">mapString</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#mask" class="sidebar-link">mask</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#pad" class="sidebar-link">pad</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#palindrome" class="sidebar-link">palindrome</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#pluralize" class="sidebar-link">pluralize</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#removenonascii" class="sidebar-link">removeNonASCII</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#reversestring" class="sidebar-link">reverseString</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#sortcharactersinstring" class="sidebar-link">sortCharactersInString</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#splitlines" class="sidebar-link">splitLines</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#stringpermutations" class="sidebar-link">stringPermutations</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#striphtmltags" class="sidebar-link">stripHTMLTags</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#tocamelcase" class="sidebar-link">toCamelCase</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#tokebabcase" class="sidebar-link">toKebabCase</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#tosnakecase" class="sidebar-link">toSnakeCase</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#totitlecase" class="sidebar-link">toTitleCase</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#truncatestring" class="sidebar-link">truncateString</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#unescapehtml" class="sidebar-link">unescapeHTML</a></li><li class="sidebar-sub-header"><a href="/frontend/utils/code-string.html#words" class="sidebar-link">words</a></li></ul></li><li><a href="/frontend/utils/code-type.html" class="sidebar-link">30s code type</a></li><li><a href="/frontend/utils/code-utility.html" class="sidebar-link">30s code utility</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><blockquote><p><a href="https://www.30secondsofcode.org/tag/string" target="_blank" rel="noopener noreferrer">30 seconds of code (String)<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></blockquote> <h2 id="csvtoarray"><a href="#csvtoarray" class="header-anchor">#</a> CSVToArray</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> CSVToArray <span class="token operator">=</span> <span class="token punctuation">(</span>data<span class="token punctuation">,</span> delimiter <span class="token operator">=</span> <span class="token string">','</span><span class="token punctuation">,</span> omitFirstRow <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  data
    <span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>omitFirstRow <span class="token operator">?</span> data<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'\n'</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'\n'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">v</span> <span class="token operator">=&gt;</span> v<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span>delimiter<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>CSV 字符串转为二维数组。判断 title 后，依次对换行符、分隔符进行划分。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">CSVToArray</span><span class="token punctuation">(</span><span class="token string">'a,b\nc,d'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [['a','b'],['c','d']];</span>
<span class="token function">CSVToArray</span><span class="token punctuation">(</span><span class="token string">'a;b\nc;d'</span><span class="token punctuation">,</span> <span class="token string">';'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [['a','b'],['c','d']];</span>
<span class="token function">CSVToArray</span><span class="token punctuation">(</span><span class="token string">'col1,col2\na,b\nc,d'</span><span class="token punctuation">,</span> <span class="token string">','</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [['a','b'],['c','d']];</span>
</code></pre></div><h2 id="csvtojson"><a href="#csvtojson" class="header-anchor">#</a> CSVToJSON</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> CSVToJSON <span class="token operator">=</span> <span class="token punctuation">(</span>data<span class="token punctuation">,</span> delimiter <span class="token operator">=</span> <span class="token string">','</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> titles <span class="token operator">=</span> data<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> data<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'\n'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span>delimiter<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> data
    <span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>data<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">'\n'</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'\n'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">v</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
      <span class="token keyword">const</span> values <span class="token operator">=</span> v<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span>delimiter<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span> titles<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">obj<span class="token punctuation">,</span> title<span class="token punctuation">,</span> index</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>obj<span class="token punctuation">[</span>title<span class="token punctuation">]</span> <span class="token operator">=</span> values<span class="token punctuation">[</span>index<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> obj<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>CSV 字符串 JSON。利用 reduce 构成 JSON 并保留了 title。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">CSVToJSON</span><span class="token punctuation">(</span><span class="token string">'col1,col2\na,b\nc,d'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];</span>
<span class="token function">CSVToJSON</span><span class="token punctuation">(</span><span class="token string">'col1;col2\na;b\nc;d'</span><span class="token punctuation">,</span> <span class="token string">';'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];</span>
</code></pre></div><h2 id="urljoin"><a href="#urljoin" class="header-anchor">#</a> URLJoin</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">URLJoin</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token operator">...</span>args</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  args
    <span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/[\/]+/g</span><span class="token punctuation">,</span> <span class="token string">'/'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/^(.+):\//</span><span class="token punctuation">,</span> <span class="token string">'$1://'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/^file:/</span><span class="token punctuation">,</span> <span class="token string">'file:/'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/\/(\?|&amp;|#[^!])/g</span><span class="token punctuation">,</span> <span class="token string">'$1'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/\?/g</span><span class="token punctuation">,</span> <span class="token string">'&amp;'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">'&amp;'</span><span class="token punctuation">,</span> <span class="token string">'?'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>URL 拼接。没看懂正则这一块。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">URLJoin</span><span class="token punctuation">(</span><span class="token string">'http://www.google.com'</span><span class="token punctuation">,</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'/b/cd'</span><span class="token punctuation">,</span> <span class="token string">'?foo=123'</span><span class="token punctuation">,</span> <span class="token string">'?bar=foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'http://www.google.com/a/b/cd?foo=123&amp;bar=foo'</span>
</code></pre></div><h2 id="bytesize"><a href="#bytesize" class="header-anchor">#</a> byteSize</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">byteSize</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> <span class="token keyword">new</span> <span class="token class-name">Blob</span><span class="token punctuation">(</span><span class="token punctuation">[</span>str<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>size<span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>获取字节的长度。四字节的字符直接使用 length 判断会错误。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token string">'😀'</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span> <span class="token comment">// 2</span>
<span class="token function">byteSize</span><span class="token punctuation">(</span><span class="token string">'😀'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 4</span>
<span class="token function">byteSize</span><span class="token punctuation">(</span><span class="token string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 11</span>
</code></pre></div><h2 id="capitalize-decapitalize"><a href="#capitalize-decapitalize" class="header-anchor">#</a> capitalize/decapitalize</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">capitalize</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">[</span>first<span class="token punctuation">,</span> <span class="token operator">...</span>rest<span class="token punctuation">]</span><span class="token punctuation">,</span> lowerRest <span class="token operator">=</span> <span class="token boolean">false</span></span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  first<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token punctuation">(</span>lowerRest <span class="token operator">?</span> rest<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> rest<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token function-variable function">decapitalize</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">[</span>first<span class="token punctuation">,</span> <span class="token operator">...</span>rest<span class="token punctuation">]</span><span class="token punctuation">,</span> upperRest <span class="token operator">=</span> <span class="token boolean">false</span></span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  first<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token punctuation">(</span>upperRest <span class="token operator">?</span> rest<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> rest<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>首字母大写、小写。利用 <code>...</code> 运算符获取剩余的参数（对象上常用该方法解构）。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">capitalize</span><span class="token punctuation">(</span><span class="token string">'fooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'FooBar'</span>
<span class="token function">capitalize</span><span class="token punctuation">(</span><span class="token string">'fooBar'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'Foobar'</span>
<span class="token function">decapitalize</span><span class="token punctuation">(</span><span class="token string">'FooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'fooBar'</span>
<span class="token function">decapitalize</span><span class="token punctuation">(</span><span class="token string">'FooBar'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'fOOBAR'</span>
</code></pre></div><h2 id="capitalizeeveryword"><a href="#capitalizeeveryword" class="header-anchor">#</a> capitalizeEveryWord</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">capitalizeEveryWord</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/\b[a-z]/g</span><span class="token punctuation">,</span> <span class="token parameter">char</span> <span class="token operator">=&gt;</span> char<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>单词首字母大写。相对于早期自用的 split 然后首字母大写，该形式匹配单词边界 <code>\b</code> 明显更优。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">capitalizeEveryWord</span><span class="token punctuation">(</span><span class="token string">'hello world!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'Hello World!'</span>
</code></pre></div><h2 id="compactwhitespace"><a href="#compactwhitespace" class="header-anchor">#</a> compactWhitespace</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">compactWhitespace</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/\s{2,}/g</span><span class="token punctuation">,</span> <span class="token string">' '</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>格式化空格符。<code>\s</code> 匹配空白符，<code>{2,}</code> 匹配大于 2 的空白符。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">compactWhitespace</span><span class="token punctuation">(</span><span class="token string">'Lorem    Ipsum'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'Lorem Ipsum'</span>
<span class="token function">compactWhitespace</span><span class="token punctuation">(</span><span class="token string">'Lorem \n Ipsum'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'Lorem Ipsum'</span>
</code></pre></div><h2 id="escapehtml"><a href="#escapehtml" class="header-anchor">#</a> escapeHTML</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">escapeHTML</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span>
  str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>
    <span class="token regex">/[&amp;&lt;&gt;'&quot;]/g</span><span class="token punctuation">,</span>
    <span class="token parameter">tag</span> <span class="token operator">=&gt;</span>
      <span class="token punctuation">(</span><span class="token punctuation">{</span>
        <span class="token string">'&amp;'</span><span class="token operator">:</span> <span class="token string">'&amp;amp;'</span><span class="token punctuation">,</span>
        <span class="token string">'&lt;'</span><span class="token operator">:</span> <span class="token string">'&amp;lt;'</span><span class="token punctuation">,</span>
        <span class="token string">'&gt;'</span><span class="token operator">:</span> <span class="token string">'&amp;gt;'</span><span class="token punctuation">,</span>
        <span class="token string">&quot;'&quot;</span><span class="token operator">:</span> <span class="token string">'&amp;#39;'</span><span class="token punctuation">,</span>
        <span class="token string">'&quot;'</span><span class="token operator">:</span> <span class="token string">'&amp;quot;'</span>
      <span class="token punctuation">}</span><span class="token punctuation">[</span>tag<span class="token punctuation">]</span> <span class="token operator">||</span> tag<span class="token punctuation">)</span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>HTML 转义（常用于防御 XSS 攻击）。利用了 replace 的第二个参数和策略模式。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">escapeHTML</span><span class="token punctuation">(</span><span class="token string">'&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;Me &amp;amp; you&amp;lt;/a&amp;gt;'</span>
</code></pre></div><h2 id="escaperegexp"><a href="#escaperegexp" class="header-anchor">#</a> escapeRegExp</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">escapeRegExp</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/[.*+?^${}()|[\]\\]/g</span><span class="token punctuation">,</span> <span class="token string">'\\$&amp;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>转义正则。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">escapeRegExp</span><span class="token punctuation">(</span><span class="token string">'(test)'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// \\(test\\)</span>
</code></pre></div><h2 id="fromcamelcase"><a href="#fromcamelcase" class="header-anchor">#</a> fromCamelCase</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> fromCamelCase <span class="token operator">=</span> <span class="token punctuation">(</span>str<span class="token punctuation">,</span> separator <span class="token operator">=</span> <span class="token string">'_'</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  str
    <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/([a-z\d])([A-Z])/g</span><span class="token punctuation">,</span> <span class="token string">'$1'</span> <span class="token operator">+</span> separator <span class="token operator">+</span> <span class="token string">'$2'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/([A-Z]+)([A-Z][a-z\d]+)/g</span><span class="token punctuation">,</span> <span class="token string">'$1'</span> <span class="token operator">+</span> separator <span class="token operator">+</span> <span class="token string">'$2'</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>从驼峰字符串格式转为其余格式。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">fromCamelCase</span><span class="token punctuation">(</span><span class="token string">'someDatabaseFieldName'</span><span class="token punctuation">,</span> <span class="token string">' '</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'some database field name'</span>
<span class="token function">fromCamelCase</span><span class="token punctuation">(</span><span class="token string">'someLabelThatNeedsToBeCamelized'</span><span class="token punctuation">,</span> <span class="token string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'some-label-that-needs-to-be-camelized'</span>
<span class="token function">fromCamelCase</span><span class="token punctuation">(</span><span class="token string">'someJavascriptProperty'</span><span class="token punctuation">,</span> <span class="token string">'_'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'some_javascript_property'</span>
</code></pre></div><h2 id="indentstring"><a href="#indentstring" class="header-anchor">#</a> indentString</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> indentString <span class="token operator">=</span> <span class="token punctuation">(</span>str<span class="token punctuation">,</span> count<span class="token punctuation">,</span> indent <span class="token operator">=</span> <span class="token string">' '</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/^/gm</span><span class="token punctuation">,</span> indent<span class="token punctuation">.</span><span class="token function">repeat</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>控制缩进。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">indentString</span><span class="token punctuation">(</span><span class="token string">'Lorem\nIpsum'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '  Lorem\n  Ipsum'</span>
<span class="token function">indentString</span><span class="token punctuation">(</span><span class="token string">'Lorem\nIpsum'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'_'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '__Lorem\n__Ipsum'</span>
</code></pre></div><h2 id="isabsoluteurl"><a href="#isabsoluteurl" class="header-anchor">#</a> isAbsoluteURL</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">isAbsoluteURL</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> <span class="token regex">/^[a-z][a-z0-9+.-]*:/</span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>判断是否为绝对路径的 URL。以 URL 协议开头。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">isAbsoluteURL</span><span class="token punctuation">(</span><span class="token string">'https://google.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token function">isAbsoluteURL</span><span class="token punctuation">(</span><span class="token string">'ftp://www.myserver.net'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token function">isAbsoluteURL</span><span class="token punctuation">(</span><span class="token string">'/foo/bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
</code></pre></div><h2 id="isanagram"><a href="#isanagram" class="header-anchor">#</a> isAnagram</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">isAnagram</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str1<span class="token punctuation">,</span> str2</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> <span class="token function-variable function">normalize</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span>
    str
      <span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
      <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/[^a-z0-9]/gi</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
      <span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span>
      <span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
      <span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token function">normalize</span><span class="token punctuation">(</span>str1<span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token function">normalize</span><span class="token punctuation">(</span>str2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>检查两串字符串是否一致（不区分大小写，忽略空格、标点、特殊字符）。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">isAnagram</span><span class="token punctuation">(</span><span class="token string">'iceman'</span><span class="token punctuation">,</span> <span class="token string">'cinema'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token function">isAnagram</span><span class="token punctuation">(</span><span class="token string">'Biceman'</span><span class="token punctuation">,</span> <span class="token string">'cinemaA'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
</code></pre></div><h2 id="islowercase-isuppercase"><a href="#islowercase-isuppercase" class="header-anchor">#</a> isLowerCase/isUpperCase</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">isLowerCase</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> str <span class="token operator">===</span> str<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token function-variable function">isUpperCase</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> str <span class="token operator">===</span> str<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>判断字符串是否为小写、大写。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">isLowerCase</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token function">isLowerCase</span><span class="token punctuation">(</span><span class="token string">'a3@$'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token function">isLowerCase</span><span class="token punctuation">(</span><span class="token string">'Ab4'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
<span class="token function">isUpperCase</span><span class="token punctuation">(</span><span class="token string">'ABC'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token function">isUpperCase</span><span class="token punctuation">(</span><span class="token string">'A3@$'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token function">isUpperCase</span><span class="token punctuation">(</span><span class="token string">'aB4'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
</code></pre></div><h2 id="mapstring"><a href="#mapstring" class="header-anchor">#</a> mapString</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">mapString</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str<span class="token punctuation">,</span> fn</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  str
    <span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">c<span class="token punctuation">,</span> i</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token function">fn</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> i<span class="token punctuation">,</span> str<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>字符串映射。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">mapString</span><span class="token punctuation">(</span><span class="token string">'lorem ipsum'</span><span class="token punctuation">,</span> <span class="token parameter">c</span> <span class="token operator">=&gt;</span> c<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'LOREM IPSUM'</span>
</code></pre></div><h2 id="mask"><a href="#mask" class="header-anchor">#</a> mask</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> mask <span class="token operator">=</span> <span class="token punctuation">(</span>cc<span class="token punctuation">,</span> num <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">,</span> mask <span class="token operator">=</span> <span class="token string">'*'</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>cc<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token operator">-</span>num<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">padStart</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>cc<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">.</span>length<span class="token punctuation">,</span> mask<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>掩盖（填充）部分字符。利用 slice 保留后面一部分字符串，padStart 填充前一部分字符。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">mask</span><span class="token punctuation">(</span><span class="token number">1234567890</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '******7890'</span>
<span class="token function">mask</span><span class="token punctuation">(</span><span class="token number">1234567890</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '*******890'</span>
<span class="token function">mask</span><span class="token punctuation">(</span><span class="token number">1234567890</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">'$'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '$$$$567890'</span>
</code></pre></div><h2 id="pad"><a href="#pad" class="header-anchor">#</a> pad</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> pad <span class="token operator">=</span> <span class="token punctuation">(</span>str<span class="token punctuation">,</span> length<span class="token punctuation">,</span> char <span class="token operator">=</span> <span class="token string">' '</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  str<span class="token punctuation">.</span><span class="token function">padStart</span><span class="token punctuation">(</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span>length <span class="token operator">+</span> length<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">,</span> char<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">padEnd</span><span class="token punctuation">(</span>length<span class="token punctuation">,</span> char<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>两侧填充字符串。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">pad</span><span class="token punctuation">(</span><span class="token string">'cat'</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '  cat   '</span>
<span class="token function">pad</span><span class="token punctuation">(</span><span class="token function">String</span><span class="token punctuation">(</span><span class="token number">42</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token string">'0'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '004200'</span>
<span class="token function">pad</span><span class="token punctuation">(</span><span class="token string">'foobar'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'foobar'</span>
</code></pre></div><h2 id="palindrome"><a href="#palindrome" class="header-anchor">#</a> palindrome</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">palindrome</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> s <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/[\W_]/g</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> s <span class="token operator">===</span> <span class="token punctuation">[</span><span class="token operator">...</span>s<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>判断是否为回文。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">palindrome</span><span class="token punctuation">(</span><span class="token string">'taco cat'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
</code></pre></div><h2 id="pluralize"><a href="#pluralize" class="header-anchor">#</a> pluralize</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> pluralize <span class="token operator">=</span> <span class="token punctuation">(</span>val<span class="token punctuation">,</span> word<span class="token punctuation">,</span> plural <span class="token operator">=</span> word <span class="token operator">+</span> <span class="token string">'s'</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> _pluralize <span class="token operator">=</span> <span class="token punctuation">(</span>num<span class="token punctuation">,</span> word<span class="token punctuation">,</span> plural <span class="token operator">=</span> word <span class="token operator">+</span> <span class="token string">'s'</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
    <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span><span class="token function">Number</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">?</span> word <span class="token operator">:</span> plural<span class="token punctuation">;</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> val <span class="token operator">===</span> <span class="token string">'object'</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token parameter">num<span class="token punctuation">,</span> word</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token function">_pluralize</span><span class="token punctuation">(</span>num<span class="token punctuation">,</span> word<span class="token punctuation">,</span> val<span class="token punctuation">[</span>word<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> <span class="token function">_pluralize</span><span class="token punctuation">(</span>val<span class="token punctuation">,</span> word<span class="token punctuation">,</span> plural<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>根据输入的数字返回对应的单词的单复数形式。当传进参数为对象时可以处理为闭包。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">pluralize</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">'apple'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'apples'</span>
<span class="token function">pluralize</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'apple'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'apple'</span>
<span class="token function">pluralize</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'apple'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'apples'</span>
<span class="token function">pluralize</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'person'</span><span class="token punctuation">,</span> <span class="token string">'people'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'people'</span>

<span class="token keyword">const</span> <span class="token constant">PLURALS</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
  person<span class="token operator">:</span> <span class="token string">'people'</span><span class="token punctuation">,</span>
  radius<span class="token operator">:</span> <span class="token string">'radii'</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> autoPluralize <span class="token operator">=</span> <span class="token function">pluralize</span><span class="token punctuation">(</span><span class="token constant">PLURALS</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">autoPluralize</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'person'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'people'</span>
</code></pre></div><h2 id="removenonascii"><a href="#removenonascii" class="header-anchor">#</a> removeNonASCII</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">removeNonASCII</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/[^\x20-\x7E]/g</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>移除不可打印的字符。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">removeNonASCII</span><span class="token punctuation">(</span><span class="token string">'äÄçÇéÉêlorem-ipsumöÖÐþúÚ'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'lorem-ipsum'</span>
</code></pre></div><h2 id="reversestring"><a href="#reversestring" class="header-anchor">#</a> reverseString</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">reverseString</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> <span class="token punctuation">[</span><span class="token operator">...</span>str<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>反转字符串。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">reverseString</span><span class="token punctuation">(</span><span class="token string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'raboof'</span>
</code></pre></div><h2 id="sortcharactersinstring"><a href="#sortcharactersinstring" class="header-anchor">#</a> sortCharactersInString</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">sortCharactersInString</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> <span class="token punctuation">[</span><span class="token operator">...</span>str<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> a<span class="token punctuation">.</span><span class="token function">localeCompare</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>字符串排序。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">sortCharactersInString</span><span class="token punctuation">(</span><span class="token string">'cabbaAge'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'aaAbbceg'</span>
</code></pre></div><h2 id="splitlines"><a href="#splitlines" class="header-anchor">#</a> splitLines</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">splitLines</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> str<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token regex">/\r?\n/</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>按行分割。匹配 Windows 和 Linux 不同的换行符。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">splitLines</span><span class="token punctuation">(</span><span class="token string">'This\nis a\nmultiline\nstring.\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// ['This', 'is a', 'multiline', 'string.' , '']</span>
</code></pre></div><h2 id="stringpermutations"><a href="#stringpermutations" class="header-anchor">#</a> stringPermutations</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">stringPermutations</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>str<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">return</span> str<span class="token punctuation">.</span>length <span class="token operator">===</span> <span class="token number">2</span> <span class="token operator">?</span> <span class="token punctuation">[</span>str<span class="token punctuation">,</span> str<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> str<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">:</span> <span class="token punctuation">[</span>str<span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> str
    <span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span>
      <span class="token punctuation">(</span><span class="token parameter">acc<span class="token punctuation">,</span> letter<span class="token punctuation">,</span> i</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
        acc<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token function">stringPermutations</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">+</span> str<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">val</span> <span class="token operator">=&gt;</span> letter <span class="token operator">+</span> val<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">[</span><span class="token punctuation">]</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>字符的排列组合。超过 8-10 个字符会导致浏览器挂起，因为主要利用了多次递归。优化 2 个字符及以下。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">stringPermutations</span><span class="token punctuation">(</span><span class="token string">'abc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// ['abc','acb','bac','bca','cab','cba']</span>
</code></pre></div><h2 id="striphtmltags"><a href="#striphtmltags" class="header-anchor">#</a> stripHTMLTags</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">stripHTMLTags</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex">/&lt;[^&gt;]*&gt;/g</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>忽略 HTML 字符。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">stripHTMLTags</span><span class="token punctuation">(</span><span class="token string">'&lt;p&gt;&lt;em&gt;lorem&lt;/em&gt; &lt;strong&gt;ipsum&lt;/strong&gt;&lt;/p&gt;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'lorem ipsum'</span>
</code></pre></div><h2 id="tocamelcase"><a href="#tocamelcase" class="header-anchor">#</a> toCamelCase</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">toCamelCase</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> s <span class="token operator">=</span>
    str <span class="token operator">&amp;&amp;</span>
    str
      <span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex">/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g</span><span class="token punctuation">)</span>
      <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> x<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> x<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> s<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> s<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>转为驼峰格式。利用正则来对多种形式进行匹配。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">toCamelCase</span><span class="token punctuation">(</span><span class="token string">'some_database_field_name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'someDatabaseFieldName'</span>
<span class="token function">toCamelCase</span><span class="token punctuation">(</span><span class="token string">'Some label that needs to be camelized'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'someLabelThatNeedsToBeCamelized'</span>
<span class="token function">toCamelCase</span><span class="token punctuation">(</span><span class="token string">'some-javascript-property'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'someJavascriptProperty'</span>
<span class="token function">toCamelCase</span><span class="token punctuation">(</span><span class="token string">'some-mixed_string with spaces_underscores-and-hyphens'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'someMixedStringWithSpacesUnderscoresAndHyphens'</span>
</code></pre></div><h2 id="tokebabcase"><a href="#tokebabcase" class="header-anchor">#</a> toKebabCase</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">toKebabCase</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span>
  str <span class="token operator">&amp;&amp;</span>
  str
    <span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex">/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> x<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>转为 kebab 格式。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">toKebabCase</span><span class="token punctuation">(</span><span class="token string">'camelCase'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'camel-case'</span>
<span class="token function">toKebabCase</span><span class="token punctuation">(</span><span class="token string">'some text'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'some-text'</span>
<span class="token function">toKebabCase</span><span class="token punctuation">(</span><span class="token string">'some-mixed_string With spaces_underscores-and-hyphens'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'some-mixed-string-with-spaces-underscores-and-hyphens'</span>
<span class="token function">toKebabCase</span><span class="token punctuation">(</span><span class="token string">'AllThe-small Things'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;all-the-small-things&quot;</span>
<span class="token function">toKebabCase</span><span class="token punctuation">(</span><span class="token string">'IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html&quot;</span>
</code></pre></div><h2 id="tosnakecase"><a href="#tosnakecase" class="header-anchor">#</a> toSnakeCase</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">toSnakeCase</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span>
  str <span class="token operator">&amp;&amp;</span>
  str
    <span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex">/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> x<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'_'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>转为 snake 格式。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">toSnakeCase</span><span class="token punctuation">(</span><span class="token string">'camelCase'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'camel_case'</span>
<span class="token function">toSnakeCase</span><span class="token punctuation">(</span><span class="token string">'some text'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'some_text'</span>
<span class="token function">toSnakeCase</span><span class="token punctuation">(</span><span class="token string">'some-mixed_string With spaces_underscores-and-hyphens'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'some_mixed_string_with_spaces_underscores_and_hyphens'</span>
<span class="token function">toSnakeCase</span><span class="token punctuation">(</span><span class="token string">'AllThe-small Things'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;all_the_smal_things&quot;</span>
<span class="token function">toSnakeCase</span><span class="token punctuation">(</span><span class="token string">'IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &quot;i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_some_xml_and_html&quot;</span>
</code></pre></div><h2 id="totitlecase"><a href="#totitlecase" class="header-anchor">#</a> toTitleCase</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">toTitleCase</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span>
  str
    <span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span><span class="token regex">/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=&gt;</span> x<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> x<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">' '</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>转为 title 格式。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">toTitleCase</span><span class="token punctuation">(</span><span class="token string">'some_database_field_name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'Some Database Field Name'</span>
<span class="token function">toTitleCase</span><span class="token punctuation">(</span><span class="token string">'Some label that needs to be title-cased'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'Some Label That Needs To Be Title Cased'</span>
<span class="token function">toTitleCase</span><span class="token punctuation">(</span><span class="token string">'some-package-name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'Some Package Name'</span>
<span class="token function">toTitleCase</span><span class="token punctuation">(</span><span class="token string">'some-mixed_string with spaces_underscores-and-hyphens'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'Some Mixed String With Spaces Underscores And Hyphens'</span>
</code></pre></div><h2 id="truncatestring"><a href="#truncatestring" class="header-anchor">#</a> truncateString</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">truncateString</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str<span class="token punctuation">,</span> num</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>
  str<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> num <span class="token operator">?</span> str<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> num <span class="token operator">&gt;</span> <span class="token number">3</span> <span class="token operator">?</span> num <span class="token operator">-</span> <span class="token number">3</span> <span class="token operator">:</span> num<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'...'</span> <span class="token operator">:</span> str<span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>省略字符串。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">truncateString</span><span class="token punctuation">(</span><span class="token string">'boomerang'</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 'boom...'</span>
</code></pre></div><h2 id="unescapehtml"><a href="#unescapehtml" class="header-anchor">#</a> unescapeHTML</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token function-variable function">unescapeHTML</span> <span class="token operator">=</span> <span class="token parameter">str</span> <span class="token operator">=&gt;</span>
  str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>
    <span class="token regex">/&amp;amp;|&amp;lt;|&amp;gt;|&amp;#39;|&amp;quot;/g</span><span class="token punctuation">,</span>
    <span class="token parameter">tag</span> <span class="token operator">=&gt;</span>
      <span class="token punctuation">(</span><span class="token punctuation">{</span>
        <span class="token string">'&amp;amp;'</span><span class="token operator">:</span> <span class="token string">'&amp;'</span><span class="token punctuation">,</span>
        <span class="token string">'&amp;lt;'</span><span class="token operator">:</span> <span class="token string">'&lt;'</span><span class="token punctuation">,</span>
        <span class="token string">'&amp;gt;'</span><span class="token operator">:</span> <span class="token string">'&gt;'</span><span class="token punctuation">,</span>
        <span class="token string">'&amp;#39;'</span><span class="token operator">:</span> <span class="token string">&quot;'&quot;</span><span class="token punctuation">,</span>
        <span class="token string">'&amp;quot;'</span><span class="token operator">:</span> <span class="token string">'&quot;'</span>
      <span class="token punctuation">}</span><span class="token punctuation">[</span>tag<span class="token punctuation">]</span> <span class="token operator">||</span> tag<span class="token punctuation">)</span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>还原转义的 HTML。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">unescapeHTML</span><span class="token punctuation">(</span><span class="token string">'&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;Me &amp;amp; you&amp;lt;/a&amp;gt;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// '&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;'</span>
</code></pre></div><h2 id="words"><a href="#words" class="header-anchor">#</a> words</h2> <p><strong>FUNCTION：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> words <span class="token operator">=</span> <span class="token punctuation">(</span>str<span class="token punctuation">,</span> pattern <span class="token operator">=</span> <span class="token regex">/[^a-zA-Z-]+/</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> str<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span>pattern<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>Boolean<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><strong>CONCEPTS：</strong></p> <p>字符串转为单词数组。</p> <p><strong>EXAMPLES：</strong></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">words</span><span class="token punctuation">(</span><span class="token string">'I love javaScript!!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [&quot;I&quot;, &quot;love&quot;, &quot;javaScript&quot;]</span>
<span class="token function">words</span><span class="token punctuation">(</span><span class="token string">'python, javaScript &amp; coffee'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// [&quot;python&quot;, &quot;javaScript&quot;, &quot;coffee&quot;]</span>
</code></pre></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2020年3月17日星期二下午2点47分</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/frontend/utils/code-object.html" class="prev">
        30s code object
      </a></span> <span class="next"><a href="/frontend/utils/code-type.html">
        30s code type
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.1f0f93c9.js" defer></script><script src="/assets/js/2.f28f3227.js" defer></script><script src="/assets/js/69.48c9a47f.js" defer></script><script src="/assets/js/3.a7eaa85f.js" defer></script>
  </body>
</html>
